package listTest;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-07-16
 * Time: 14:21
 */
public class MyArray implements IList{
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyArray() {
        this.array = new int[DEFAULT_CAPACITY];
    }
    public boolean isFull() {
        return this.usedSize == array.length;
    }
    private void expand() {
        this.array = Arrays.copyOf(this.array,
                this.array.length*2);
    }
    @Override
    public void add(int data) {
        if(isFull()) {
           expand();
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }
    public void checkPos(int pos) throws PosIllegalException{
        if(pos < 0 || pos > this.usedSize) {
            throw new PosIllegalException("pos位置不合法");
        }

    }
    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if (isFull()) {
                expand();
            }
            for (int i = this.usedSize-1;i >=pos ;i--){
                array[i+1] = array[i];
            }
            array[pos] = data;
            usedSize++;
        }catch (PosIllegalException e) {
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i =0;i < this.usedSize;i++) {
            if(array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i =0;i < this.usedSize;i++) {
            if(array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    public void checkPos2(int pos) throws PosIllegalException{
        if(pos < 0 || pos >= this.usedSize) {
            throw new PosIllegalException("pos位置不合法");
        }
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }

    public void checkEmpty() throws EmptyException{
        if(isEmpty()) {
            throw new EmptyException("顺序表为空");
        }
    }
    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            checkPos(pos);
            return this.array[pos];
        }catch (PosIllegalException e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    //更新数据
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos2(pos);
            array[pos] = value;
        }catch (PosIllegalException e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if (pos == -1) {
                return;
            }
            for (int i = pos;i < usedSize-1;i++) {
                array[i] = array[i+1];
            }
            usedSize--;
        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        /*for (int i = 0;i < this.usedSize;i++) {
            array[i] = null;
        }*/
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
